package com.jeesite.modules.dataScreen.web;

import com.alibaba.fastjson2.JSONObject;
import com.jeesite.modules.dataScreen.service.PostalService;
import com.jeesite.modules.dataScreen.service.TianBaoService;
import com.jeesite.modules.dataScreen.web.vo.BasicPostalVO;
import com.jeesite.modules.dataScreen.web.vo.Result;
import com.jeesite.modules.dataScreen.web.vo.postal.PostMonthDataVo;
import com.jeesite.modules.dataScreen.web.vo.postal.PostalDataVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/postalData")
@Api(tags = "邮政")
public class PostalDataApi {

    @Resource
    private PostalService postalService;
    @Autowired
    private TianBaoService tianBaoService;

    @ApiOperation("业务量")
    @GetMapping("/serviceNumber")
    public Result<PostalDataVo> getServiceNumber(@RequestParam(defaultValue = "2024") int year){
        return Result.OK(postalService.getServiceNumber(year));
    }

    @ApiOperation("业务量占比")
    @GetMapping("/percentNumber")
    public Result<PostalDataVo> getPercentNumber(@RequestParam(defaultValue = "2024") int year) {
        return Result.OK(postalService.getPercentNumber(year));
    }

    @ApiOperation("业务收入")
    @GetMapping("/expressRevenue")
    public Result<HashMap<String,Object>> getExpressRevenue(@RequestParam(defaultValue = "2024") int year) {
        List<PostMonthDataVo> postMonthData = postalService.getExpressRevenue(year);
        HashMap<String, Object> hashMap = new HashMap<>();

        List<PostMonthDataVo> filteredData = postMonthData.stream()
                .filter(dateVo -> dateVo.getYear() == year)
                .collect(Collectors.toList());

        BigDecimal totalRevenue = filteredData.stream()
                .map(PostMonthDataVo::getPostalRevenue)
                .filter(Objects::nonNull)
                .map(BigDecimal::new)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        BigDecimal totalER = filteredData.stream()
                .map(PostMonthDataVo::getExpressRevenue)
                .filter(Objects::nonNull)
                .map(BigDecimal::new)
                .reduce(BigDecimal.ZERO, BigDecimal::add);

        hashMap.put("totalPostalRevenue", totalRevenue.toString());
        hashMap.put("totalExpressRevenue", totalER.toString());
        hashMap.put("monthData", postMonthData);

        return Result.OK(hashMap);
    }

    @ApiOperation("月走势")
    @GetMapping("/monthlyTrend")
    public Result<List<PostMonthDataVo>> getMonthlyTrend(@RequestParam(defaultValue = "2024") int year) {

        return Result.OK(postalService.getMonthlyTrend(year));
    }

//    @ApiOperation("基础信息")
//    @GetMapping("/basicInformation")
//    public Result<Map<String,String>> basicInformation(@RequestParam(defaultValue = "2024") int year) {
//
//        return Result.OK(postalService.basicInformation(year));
//    }

    @ApiOperation("业务预警")
    @GetMapping("/businessWarning")
    public Result<Map<String,String>> businessWarning() {

        String investmentProgress = tianBaoService.getIndexValue("邮政投资完成率");
        String investFinish = tianBaoService.getIndexValue("快递业务完成率");
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("investmentProgress",investmentProgress);
        resultMap.put("investFinish",investFinish);

        return Result.OK(resultMap);
    }

    @ApiOperation("邮政网点覆盖率")
    @GetMapping("/dotCoverage")
    public Result<Map<String,String>> dotCoverage(@RequestParam(defaultValue = "2024") int year) {

        String dotCoverage = tianBaoService.getIndexValue("邮政网点覆盖率");
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("dotCoverage",dotCoverage);
        return Result.OK(resultMap);
    }

//    @ApiOperation("综合信息")
//    @GetMapping("/comprehensiveInformation")
//    public Result<JSONObject> comprehensiveInformation(@RequestParam(defaultValue = "服务营业场所") String type) {
//
//        return Result.OK(postalService.comprehensiveInformation(type));
//    }

    @GetMapping("/basicPostal")
    @ApiOperation("邮政基础")
    public Result<BasicPostalVO> basicPostal() {
        return Result.OK(postalService.basicPostal());
    }

    @GetMapping("/businessPremises")
    @ApiOperation("营业场所")
    public Result<List<JSONObject>> businessPremises() {
        return Result.OK(postalService.businessPremises());
    }

    @GetMapping("/expressEnterprise")
    @ApiOperation("快递企业")
    public Result<List<String>> expressEnterprise() {
        return Result.OK(postalService.expressEnterprise());
    }

    @GetMapping("/businessPopUp")
    @ApiOperation("营业场所弹窗")
    @ApiImplicitParam(name = "name",value = "区县名称")
    public Result<List<JSONObject>> businessPopUp(@RequestParam(required = false) String name) {
        return Result.OK(postalService.businessPopUp(name));
    }

    @GetMapping("/enterprisePopUp")
    @ApiOperation("快递企业弹窗")
    @ApiImplicitParam(name = "name",value = "品牌名称")
    public Result<List<JSONObject>> enterprisePopUp(@RequestParam(required = false) String name) {
        return Result.OK(postalService.enterprisePopUp(name));
    }

    @GetMapping("/brandClassification")
    @ApiOperation("品牌分类")
    public Result<List<JSONObject>> brandClassification(String name) {
        return Result.OK(postalService.brandClassification(name));
    }
}
